home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 January: Mac OS SDK / Dev.CD Jan 98 SDK1.toast / Development Kits (Disc 1) / Apple Game Sprockets / InputSprocket 1.1 / InputSprocketTest Sources / InputSprocketSimpleTest.cp next >
Encoding:
Text File  |  1996-12-18  |  42.7 KB  |  2,116 lines  |  [TEXT/CWIE]

  1. #ifndef DEBUG_DRIVER
  2.     #define DEBUG_DRIVER 0
  3. #endif
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7.  
  8. #include <DeskBus.h>
  9.  
  10. #include "InputSprocket.h"
  11. #include "SIOUX.h"
  12. #include <CursorDevices.h>
  13. #include <LowMem.h>
  14.  
  15. // which test (AUTOTEST, DIALOGTEST or MENUTEST
  16. #define AUTOTEST        1
  17.  
  18. // set the other #defines to 0
  19. #ifndef AUTOTEST
  20. #define AUTOTEST        0
  21. #endif
  22.  
  23. #ifndef DIALOGTEST
  24. #define DIALOGTEST    0
  25. #endif
  26.  
  27. #ifndef    MENUTEST
  28. #define MENUTEST        0
  29. #endif
  30.  
  31. #if DEBUG_DRIVER
  32.     #include "InputSprocketDriver.h"
  33. #endif
  34.  
  35. enum
  36. {
  37.     kNeedCount = 9
  38. };
  39.  
  40. enum
  41. {
  42.     kIconSuiteID_XThrust = 128, 
  43.     kIconSuiteID_YThrust,
  44.     kIconSuiteID_ZThrust,
  45.     kIconSuiteID_Look,
  46.     kIconSuiteID_Fire,
  47.     kIconSuiteID_Thrust,
  48.     kIconSuiteID_Pause,
  49.     kIconSuiteID_Start,
  50.     kIconSuiteID_Scroll
  51. };
  52.  
  53. Boolean gIsSuspended = false;
  54. Boolean gKeyboardEnabled = false;
  55. ISpElementListReference gVirtualList = NULL;
  56. ISpElementReference gVirtualElements[kNeedCount] = {nil, nil, nil, nil, nil, nil, nil, nil, nil};
  57.       
  58.  
  59.  
  60. static ISpNeed gNeeds[kNeedCount] =
  61. {
  62.     { "\pForward Thrust",    kIconSuiteID_YThrust,     0,    kISpElementKind_Axis,        kISpElementLabel_YAxis,    0                            },
  63.     { "\pSide Thrust",         kIconSuiteID_XThrust,    0,    kISpElementKind_Axis,        kISpElementLabel_XAxis,    0                            },
  64.     { "\pVertical Thrust",     kIconSuiteID_ZThrust,     0,    kISpElementKind_Axis,        kISpElementLabel_ZAxis,    0                            },
  65.     { "\pLook",             kIconSuiteID_Look,        0,    kISpElementKind_Movement,    kISpElementLabel_None,    0                            },
  66.     { "\pFire",             kIconSuiteID_Fire,        0,    kISpElementKind_Button,        kISpElementLabel_Fire,    0                            },
  67.     { "\pThrust",             kIconSuiteID_Thrust,     0,    kISpElementKind_Button,        kISpElementLabel_Fire,    0                            },
  68.     { "\pPause",             kIconSuiteID_Pause,        0,    kISpElementKind_Button,        kISpElementLabel_None,    kISpNeedFlag_NoMultiConfig    },
  69.     { "\pStart/Stop",         kIconSuiteID_Start,     0,    kISpElementKind_Button,        kISpElementLabel_Start,    kISpNeedFlag_NoMultiConfig    },
  70.     { "\pScroll",             kIconSuiteID_Scroll,    0,    kISpElementKind_DPad,        kISpElementLabel_None,    0                            }
  71. };
  72.  
  73. SInt32 gStep;
  74. char gStepId[255];
  75.  
  76.  
  77. typedef Boolean (*ISpEventProcPtr) (EventRecord* inEvent);
  78.  
  79. Boolean ISpConfigureFilterProc(EventRecord *inEvent);
  80.  
  81. Boolean ISpConfigureFilterProc(EventRecord *inEvent)
  82. {
  83.     Boolean handled;
  84.     
  85.     handled = SIOUXHandleOneEvent(inEvent);
  86.  
  87.     return handled;
  88. }
  89.  
  90. static void InitStep(char *msg)
  91. {
  92.     gStep = 0;
  93.     sprintf(gStepId, "%3d");
  94.     
  95.     if (msg == nil)
  96.     {
  97.         printf("step %s\n", gStepId);
  98.     }
  99.     else
  100.     {
  101.         printf("step %s %s\n",gStepId, msg);
  102.     }
  103. }
  104.  
  105. static void NextStep(char *msg)
  106. {
  107.     long this_app_memory;
  108.     long this_sys_memory;
  109.     static Boolean first_time = true;
  110.     static long last_app_memory;
  111.     static long last_sys_memory;
  112.  
  113.     gStep++;
  114.     sprintf(gStepId, "%3d", gStep);
  115.  
  116.     this_app_memory = FreeMem();
  117.     this_sys_memory = FreeMemSys();
  118.  
  119.     if (first_time)    
  120.     {
  121.         first_time = false;
  122.     }
  123.     else
  124.     {
  125.         long delta_app_memory = last_app_memory - this_app_memory;
  126.         long delta_sys_memory = last_sys_memory - this_sys_memory;
  127.         
  128.         printf("step %s ending app mem = %d sys mem = %d\n", gStepId, this_app_memory, this_sys_memory);
  129.         printf("step %s delta app mem = %d sys mem = %d\n", gStepId, delta_app_memory, delta_sys_memory);
  130.     }
  131.  
  132.     if (msg == nil)
  133.     {
  134.         printf("step %s\n", gStepId);
  135.     }
  136.     else
  137.     {
  138.         printf("step %s %s\n",gStepId, msg);
  139.     }
  140.  
  141.     printf("step %s starting app mem = %d sys mem = %d\n", gStepId, this_app_memory, this_sys_memory);
  142.  
  143.     last_app_memory = this_app_memory;
  144.     last_sys_memory = this_sys_memory;
  145. }
  146.  
  147.  
  148.  
  149. static void FailMsg(char *failure)
  150. {
  151.     printf("step %s FAILED reason = %s\n", gStepId, failure);
  152. }
  153.  
  154. static void FailCode(OSStatus errorCode)
  155. {
  156.     printf("step %s FAILED errorCode = %d\n", gStepId, errorCode);
  157. }
  158.  
  159.  
  160. #define FAILMSG(x) { FailMsg(x); return; }
  161. #define FAILCODE(x) { FailCode(x); return; }
  162.  
  163. #define FFAILMSG(x) { FailMsg(x); return false; }
  164. #define FFAILCODE(x) { FailCode(x); return false; }
  165.  
  166. static void StatusMsg(char *msg)
  167. {
  168.     printf("step %s status msg = %s\n",gStepId, msg);
  169. }
  170.  
  171. void ShowFourByte(OSType fourByte);
  172. void ShowFourByte(OSType fourByte)
  173. {
  174.     putchar(((fourByte & 0xff000000) >> 24));
  175.     putchar(((fourByte & 0x00ff0000) >> 16));
  176.     putchar(((fourByte & 0x0000ff00) >> 8));
  177.     putchar(((fourByte & 0x000000ff) >> 00));
  178. }
  179.  
  180. void ShowStr63(const Str63 &theStr);
  181. void ShowStr63(const Str63 &theStr)
  182. {
  183.     int i;
  184.     int len = theStr[0];
  185.     if (len > 63) { len = 63; }
  186.     
  187.     for(i=1; i <= len; i++)
  188.     {
  189.         putchar(theStr[i]);
  190.     }
  191. }
  192.  
  193. static void AppendStr63(char *string, const Str63 &theStr)
  194. {
  195.     char *c = string;
  196.     int i;
  197.     
  198.     // find the end of the string
  199.     while(*c != nil) { c++; }
  200.     
  201.     for(i = 1; i <= theStr[0]; i++)
  202.     {
  203.         *c = theStr[i];
  204.         c++;
  205.     }
  206.     
  207.     *c = 0;
  208. }
  209.  
  210. void PrintEvent(ISpElementEventPtr theEvent);
  211. void PrintEvent(ISpElementEventPtr theEvent)
  212. {
  213.     ISpElementInfo info;
  214.     ISpElement_GetInfo(theEvent->element, &info);
  215.     OSStatus status;
  216.     const char* s = "•UNKNOWN•";
  217.     
  218.     printf("when = [%lu] [%lu]\n", theEvent->when.hi, theEvent->when.lo);
  219.     
  220.     switch (info.theKind)
  221.     {
  222.         case kISpElementKind_Button:
  223.             switch (theEvent->data)
  224.             {
  225.                 case 0:
  226.                     s = "released";
  227.                 break;
  228.                 
  229.                 case 1:
  230.                     s = "pressed";
  231.                 break;
  232.             }
  233.             
  234.             printf("data = 0x%lx (%s)\n", theEvent->data, s);
  235.         break;
  236.         
  237.         case kISpElementKind_DPad:
  238.             switch (theEvent->data)
  239.             {
  240.                 case kISpPadIdle:
  241.                     s = "kISpPadIdle";
  242.                 break;
  243.                 
  244.                 case kISpPadLeft:
  245.                     s = "kISpPadLeft";
  246.                 break;
  247.                 
  248.                 case kISpPadUpLeft:
  249.                     s = "kISpPadUpLeft";
  250.                 break;
  251.                 
  252.                 case kISpPadUp:
  253.                     s = "kISpPadUp";
  254.                 break;
  255.                 
  256.                 case kISpPadUpRight:
  257.                     s = "kISpPadUpRight";
  258.                 break;
  259.                 
  260.                 case kISpPadRight:
  261.                     s = "kISpPadRight";
  262.                 break;
  263.                 
  264.                 case kISpPadDownRight:
  265.                     s = "kISpPadDownRight";
  266.                 break;
  267.                 
  268.                 case kISpPadDown:
  269.                     s = "kISpPadDown";
  270.                 break;
  271.                 
  272.                 case kISpPadDownLeft:
  273.                     s = "kISpPadDownLeft";
  274.                 break;
  275.             }
  276.             
  277.             printf("data = 0x%lx (%s)\n", theEvent->data, s);
  278.         break;
  279.         
  280.         case kISpElementKind_Axis:
  281.             float temp1 = ((float) theEvent->data)/((float) 0xFFFFFFFFU);
  282.             float temp2 = 2.0*temp1-1.0;
  283.             
  284.             printf("data = 0x%lx (%f%) (%f%)\n", theEvent->data, temp1, temp2);
  285.         break;
  286.         
  287.         case kISpElementKind_Movement:
  288.             printf("data = 0x%lx\n", theEvent->data);
  289.             
  290.             ISpMovementData movementData;
  291.             if (ISpElement_GetComplexState(theEvent->element, sizeof(ISpMovementData), &movementData) == noErr)
  292.             {
  293.                 switch (movementData.direction)
  294.                 {
  295.                     case kISpPadIdle:
  296.                         s = "kISpPadIdle";
  297.                     break;
  298.                     
  299.                     case kISpPadLeft:
  300.                         s = "kISpPadLeft";
  301.                     break;
  302.                     
  303.                     case kISpPadUpLeft:
  304.                         s = "kISpPadUpLeft";
  305.                     break;
  306.                     
  307.                     case kISpPadUp:
  308.                         s = "kISpPadUp";
  309.                     break;
  310.                     
  311.                     case kISpPadUpRight:
  312.                         s = "kISpPadUpRight";
  313.                     break;
  314.                     
  315.                     case kISpPadRight:
  316.                         s = "kISpPadRight";
  317.                     break;
  318.                     
  319.                     case kISpPadDownRight:
  320.                         s = "kISpPadDownRight";
  321.                     break;
  322.                     
  323.                     case kISpPadDown:
  324.                         s = "kISpPadDown";
  325.                     break;
  326.                     
  327.                     case kISpPadDownLeft:
  328.                         s = "kISpPadDownLeft";
  329.                     break;
  330.                 }
  331.                 
  332.                 float horz = 2.0*(((float) movementData.xAxis)/((float) 0xFFFFFFFFU))-1.0;
  333.                 float vert = 2.0*(((float) movementData.yAxis)/((float) 0xFFFFFFFFU))-1.0;
  334.                 
  335.                 printf("state = (%f,%f) (%s)\n", horz, vert, s);
  336.             }
  337.         break;
  338.     }
  339.     
  340.     printf("element = 0x%lx\n", theEvent->element);
  341.     printf("refcon = %lx\n", theEvent->refCon);
  342.     
  343.     printf("label = ");
  344.     ShowFourByte(info.theLabel);
  345.     printf("\n");
  346.     
  347.     printf("kind = ");
  348.     ShowFourByte(info.theKind);
  349.     printf("\n");
  350.     
  351.     printf("string = ");
  352.     ShowStr63(info.theString);
  353.     printf("\n");
  354.     
  355.     {
  356.         UInt32 data;
  357.  
  358.         status = ISpElement_GetSimpleState(theEvent->element, &data);
  359.  
  360.         if (status == noErr) { printf("polled = %x\n",data); }
  361.     }
  362.     
  363.     printf("\n\n");
  364. }
  365.  
  366.  
  367. void PrintElementBlock(ISpElementReference *theElementReferences, UInt32 count);
  368. void PrintElementBlock(ISpElementReference *theElementReferences, UInt32 count)
  369. {
  370.     printf("count = %d\n");
  371.  
  372.     int itr;
  373.     
  374.     for(itr = 0; itr < count; itr++)
  375.     {
  376.         printf("    element #%d\n",itr);
  377.         
  378.         ISpElementInfo info;
  379.  
  380.         ISpElement_GetInfo(theElementReferences[itr], &info);
  381.  
  382.         printf("        label = ");
  383.         ShowFourByte(info.theLabel);
  384.         printf("\n");
  385.  
  386.         printf("        kind = ");
  387.         ShowFourByte(info.theKind);
  388.         printf("\n");
  389.  
  390.         printf("        string = ");
  391.         ShowStr63(info.theString);
  392.         printf("\n\n");
  393.     }
  394. }
  395.  
  396. // if whichList is NULL then we use the global list
  397. static void GetAndPrintEvents(ISpElementListReference whichList)
  398. {
  399.     OSErr err;
  400.     
  401.     if (whichList == NULL)
  402.     {
  403.         err = ISpGetGlobalElementList(&whichList);
  404.         if (err)
  405.         {
  406.             printf("••• ISpGetGlobalElementList returned error (%ld)\n",err);
  407.             return;
  408.         }
  409.     }
  410.  
  411.     err = ISpElementList_Flush(whichList);
  412.     if (err)
  413.     {
  414.         printf("••• ISpElementList_Flush returned error (%ld)\n",err);
  415.         return;
  416.     }
  417.  
  418.     printf("getting events (press command to end)\n");
  419.     
  420.     while(1)
  421.     {
  422.         ISpElementEvent event;
  423.         Boolean wasEvent;
  424.  
  425.         err = ISpElementList_GetNextEvent(whichList, sizeof(event), &event, &wasEvent);
  426.         if (err)
  427.         {
  428.             printf("••• ISpElementList_GetNextEvent returned error (%ld)\n",err);
  429.             return;
  430.         }
  431.  
  432.         if (wasEvent)
  433.         {
  434.             PrintEvent(&event);
  435.         }
  436.         
  437.         KeyMap theKeys;
  438.         GetKeys(theKeys);
  439.  
  440.         if ((theKeys[1] & 0x8000))
  441.         {
  442.             break;
  443.         }
  444.         
  445.         SIOUXHandleOneEvent(nil);
  446.       }
  447. }
  448.  
  449. static void TestListOfDevices(void)
  450. {
  451.     ISpDeviceReference theDevices[100];
  452.     UInt32 deviceCount;
  453.     UInt32 deviceBufferSize = 100;
  454.     UInt32 deviceItr;
  455.     OSErr err;
  456.     
  457.     err = ISpDevices_Extract(deviceBufferSize, &deviceCount, theDevices);
  458.     if (err)
  459.     {
  460.         printf("••• ISpDevices_Extract returned error (%ld)\n",err);
  461.         return;
  462.     }
  463.     
  464.     printf("# of devices = %ld\n",deviceCount);
  465.     for(deviceItr = 0; deviceItr < deviceCount; deviceItr++)
  466.     {
  467.         printf("    device #%ld\n",deviceItr);
  468.         ISpDeviceDefinition theDfn;
  469.         
  470.         err = ISpDevice_GetDefinition(theDevices[deviceItr], sizeof(ISpDeviceDefinition), &theDfn);
  471.         if (err)
  472.         {
  473.             printf("••• ISpDevice_GetDefinition returned error (%ld)\n",err);
  474.             return;
  475.         }
  476.         
  477.         printf("        name = ");
  478.         ShowStr63(theDfn.deviceName);
  479.         printf("\n");
  480.         
  481.         printf("        class = ");
  482.         ShowFourByte(theDfn.theDeviceClass);
  483.         printf("\n");
  484.  
  485.         printf("        device identifier = ");
  486.         ShowFourByte(theDfn.theDeviceIdentifier);
  487.         printf("\n");
  488.  
  489.         printf("        permanent id = %ld\n",theDfn.permanentID);
  490.         
  491.         printf("\n\n");
  492.     }
  493. }
  494.  
  495.  
  496. static void TestListOfElements(void)
  497. {
  498.     ISpElementListReference globalList;
  499.     OSErr err;
  500.     
  501.     err = ISpGetGlobalElementList(&globalList);
  502.     if (err)
  503.     {
  504.         printf("••• ISpGetGlobalElementList returned error (%ld)\n",err);
  505.         return;
  506.     }
  507.     
  508.     ISpElementReference theElementReferences[200];
  509.     UInt32 bufferSize = 200;
  510.     UInt32 count;
  511.  
  512.     err = ISpElementList_Extract(globalList, bufferSize, &count, theElementReferences);
  513.     if (err)
  514.     {
  515.         printf("••• ISpElementList_Extract returned error (%ld)\n",err);
  516.         return;
  517.     }
  518.     
  519.     PrintElementBlock(theElementReferences, count);
  520. }
  521.  
  522.  
  523. static void TestListOfButtons(void)
  524. {
  525.     ISpElementListReference globalList;
  526.     OSErr err;
  527.     
  528.     err = ISpGetGlobalElementList(&globalList);
  529.     if (err)
  530.     {
  531.         printf("••• ISpGetGlobalElementList returned error (%ld)\n",err);
  532.         return;
  533.     }
  534.     
  535.     ISpElementReference theElementReferences[200];
  536.     UInt32 bufferSize = 200;
  537.     UInt32 count;
  538.  
  539.     err = ISpElementList_ExtractByKind(globalList, kISpElementKind_Button, bufferSize, &count, theElementReferences);
  540.     if (err)
  541.     {
  542.         printf("••• ISpElementList_Extract returned error (%ld)\n",err);
  543.         return;
  544.     }
  545.     
  546.     PrintElementBlock(theElementReferences, count);
  547. }
  548.  
  549.  
  550. static void TestListOfDPads(void)
  551. {
  552.     ISpElementListReference globalList;
  553.     OSErr err;
  554.     
  555.     err = ISpGetGlobalElementList(&globalList);
  556.     if (err)
  557.     {
  558.         printf("••• ISpGetGlobalElementList returned error (%ld)\n",err);
  559.         return;
  560.     }
  561.     
  562.     ISpElementReference theElementReferences[200];
  563.     UInt32 bufferSize = 200;
  564.     UInt32 count;
  565.  
  566.     err = ISpElementList_ExtractByKind(globalList, kISpElementKind_DPad, bufferSize, &count, theElementReferences);
  567.     if (err)
  568.     {
  569.         printf("••• ISpElementList_Extract returned error (%ld)\n",err);
  570.         return;
  571.     }
  572.     
  573.     PrintElementBlock(theElementReferences, count);
  574. }
  575.  
  576.  
  577. static void TestListOfAxes(void)
  578. {
  579.     ISpElementListReference globalList;
  580.     OSErr err;
  581.     
  582.     err = ISpGetGlobalElementList(&globalList);
  583.     if (err)
  584.     {
  585.         printf("••• ISpGetGlobalElementList returned error (%ld)\n",err);
  586.         return;
  587.     }
  588.     
  589.     ISpElementReference theElementReferences[200];
  590.     UInt32 bufferSize = 200;
  591.     UInt32 count;
  592.  
  593.     err = ISpElementList_ExtractByKind(globalList, kISpElementKind_Axis, bufferSize, &count, theElementReferences);
  594.     if (err)
  595.     {
  596.         printf("••• ISpElementList_Extract returned error (%ld)\n",err);
  597.         return;
  598.     }
  599.     
  600.     PrintElementBlock(theElementReferences, count);
  601. }
  602.  
  603.  
  604. static void TestListOfXAxes(void)
  605. {
  606.     ISpElementListReference globalList;
  607.     OSErr err;
  608.     
  609.     err = ISpGetGlobalElementList(&globalList);
  610.     if (err)
  611.     {
  612.         printf("••• ISpGetGlobalElementList returned error (%ld)\n",err);
  613.         return;
  614.     }
  615.     
  616.     ISpElementReference theElementReferences[200];
  617.     UInt32 bufferSize = 200;
  618.     UInt32 count;
  619.  
  620.     err = ISpElementList_ExtractByLabel(globalList, kISpElementLabel_XAxis, bufferSize, &count, theElementReferences);
  621.     if (err)
  622.     {
  623.         printf("••• ISpElementList_Extract returned error (%ld)\n",err);
  624.         return;
  625.     }
  626.     
  627.     PrintElementBlock(theElementReferences, count);
  628. }
  629.  
  630.  
  631. static void TestSuspend(void)
  632. {
  633.     OSErr err;
  634.     
  635.     err = ISpSuspend();
  636.     if (err)
  637.     {
  638.         printf("••• ISpSuspend returned error (%ld)\n",err);
  639.         return;
  640.     }
  641.     
  642.     gIsSuspended = true;
  643. }
  644.  
  645.  
  646. static void TestResume(void)
  647. {
  648.     OSErr err;
  649.     
  650.     err = ISpResume();
  651.     if (err)
  652.     {
  653.         printf("••• ISpResume returned error (%ld)\n",err);
  654.         return;
  655.     }
  656.     
  657.     gIsSuspended = false;
  658. }
  659.  
  660.  
  661. static void TestCreateNeeds(void)
  662. {
  663.     OSErr err;
  664.     
  665.     if (gVirtualList == NULL)
  666.     {
  667.         printf("creating virtual elements\n");
  668.         
  669.           err = ISpElement_NewVirtualFromNeeds(kNeedCount, gNeeds, gVirtualElements, 0);
  670.         if (err)
  671.         {
  672.             printf("••• ISpElement_NewVirtualFromNeeds returned error (%ld)\n",err);
  673.             return;
  674.         }
  675.         
  676.         err = ISpElementList_New(
  677.             kNeedCount,                // count
  678.             gVirtualElements,        // needs
  679.             &gVirtualList,            // virtual elements
  680.             0);                        // flags
  681.         if (err)
  682.         {
  683.             printf("••• ISpElementList_New returned error (%ld)\n",err);
  684.             return;
  685.         }
  686.               
  687.         err = ISpInit(kNeedCount,    // count
  688.                 gNeeds,                // needs
  689.                 gVirtualElements,    // virtual elements
  690.                 'ISpT',                // app
  691.                 'foob',                // sub
  692.                 0,                    // flags
  693.                 128,                // set list resource id
  694.                 0);                    // version
  695.         if (err)
  696.         {
  697.             printf("••• ISpInit returned error (%ld)\n",err);
  698.             return;
  699.         }
  700.     }
  701. }
  702.  
  703.  
  704. static void TestDestroyNeeds(void)
  705. {
  706.     OSErr err;
  707.     
  708.     if (gVirtualList != NULL)
  709.     {
  710.         err = ISpElementList_Dispose(gVirtualList);
  711.         gVirtualList = NULL;
  712.         if (err)
  713.         {
  714.             printf("••• ISpElementList_Dispose returned error (%ld)\n",err);
  715.             return;
  716.         }
  717.         
  718.          err = ISpStop();
  719.         if (err)
  720.         {
  721.             printf("••• ISpStop returned error (%ld)\n",err);
  722.             return;
  723.         }
  724.         
  725.         err = ISpElement_DisposeVirtual(kNeedCount, gVirtualElements);
  726.         if (err)
  727.         {
  728.             printf("••• ISpElement_DisposeVirtual returned error (%ld)\n",err);
  729.             return;
  730.         }
  731.     }
  732. }
  733.  
  734.  
  735. static void TestNeedsEvents(void)
  736. {
  737.     TestCreateNeeds();
  738.     
  739.     if (gVirtualList != NULL)
  740.     {
  741.         GetAndPrintEvents(gVirtualList);
  742.     }
  743. }
  744.  
  745.  
  746. static void TestConfiguration(void)
  747. {
  748.     OSErr err;
  749.     
  750.     TestCreateNeeds();
  751.     
  752.     err = ISpConfigure(nil);
  753.     if (err)
  754.     {
  755.         printf("••• ISpConfigure returned error (%ld)\n",err);
  756.         return;
  757.     }
  758. }
  759.  
  760.  
  761. static void EnableDeviceClass(OSType deviceClass, Boolean enable)
  762. {
  763.     enum {
  764.         kDeviceList_COUNT = 100
  765.     };
  766.     
  767.     OSStatus err;
  768.     UInt32 count;
  769.     ISpDeviceReference deviceList[kDeviceList_COUNT];
  770.     
  771.     // NOTE: This is not the correct way to handle the list count thing.  We
  772.     // should actually call once with NULL for the device list, malloc a list of
  773.     // that size, and call again.
  774.     
  775.     err = ISpDevices_ExtractByClass(
  776.             deviceClass,
  777.             kDeviceList_COUNT,
  778.             &count,
  779.             deviceList);
  780.     
  781.     if (err)
  782.     {
  783.         printf("••• ISpDevices_ExtractByClass returned error (%ld)\n",err);
  784.         return;
  785.     }
  786.     
  787.     if (count > kDeviceList_COUNT)
  788.     {
  789.         count = kDeviceList_COUNT;
  790.     }
  791.     
  792.     if (enable)
  793.     {
  794.         err = ISpDevices_Activate(
  795.                 count,
  796.                 deviceList);
  797.         
  798.         if (err)
  799.         {
  800.             printf("••• ISpDevices_Activate returned error (%ld)\n",err);
  801.             return;
  802.         }
  803.     }
  804.     else
  805.     {
  806.         err = ISpDevices_Deactivate(
  807.                 count,
  808.                 deviceList);
  809.         
  810.         if (err)
  811.         {
  812.             printf("••• ISpDevices_Deactivate returned error (%ld)\n",err);
  813.             return;
  814.         }
  815.     }
  816.     
  817.     if (deviceClass == kISpDeviceClass_Keyboard)
  818.     {
  819.         gKeyboardEnabled = enable;
  820.     }
  821. }
  822.  
  823. static void TestAllocateDeallocate(void)
  824. {
  825.     OSErr err = noErr;
  826.     UInt32 count = 0;
  827.     char theString[32];
  828.     UInt32 itr;    
  829.     ISpElementReference virtuals[kNeedCount];
  830.     Boolean done = false;
  831.     
  832.     printf("allocate/dellocate how many times>");
  833.     
  834.     gets( theString );
  835.     count = atoi( theString );
  836.  
  837.     printf("allocating/deallocating %ld times\n",count);
  838.     
  839.     for(itr = 0; itr < count; itr++)
  840.     {
  841.         if ((itr % 100) == 0) { putchar('.'); fflush(stdout);  }
  842.         if ((itr % 1000) == 0) 
  843.         {
  844.             long normal = FreeMem();
  845.             long system = FreeMemSys();
  846.             printf("%ld / %ld (app = %ld) (sys = %ld) \n",itr,count,normal,system);
  847.         }
  848.     
  849.         SIOUXHandleOneEvent(nil);
  850.         
  851.           err = ISpElement_NewVirtualFromNeeds(kNeedCount, gNeeds, virtuals, 0);
  852.         
  853.         if (err)
  854.         {
  855.             printf("••• itr = %d ISpElement_NewVirtualFromNeeds returned error (%ld)\n",itr, err);
  856.             
  857.             done = true;
  858.         }
  859.     
  860.         err = ISpElement_DisposeVirtual(kNeedCount, virtuals);
  861.     
  862.         if (err)
  863.         {
  864.             printf("••• ISpElementList_Dispose returned error (%ld)\n",err);
  865.             return;
  866.         }
  867.         
  868.         if (done) { return; }
  869.     }
  870. }
  871.  
  872. #define MAX_ELEMENTS    1000
  873. #define MAX_DATASIZE    1000
  874.  
  875.  
  876. #if DEBUG_DRIVER
  877. static void TestAllocatePushData(void)
  878. {
  879.     char theString[32];
  880.     OSErr err = noErr;
  881.     UInt32 elementCount = 0;
  882.     UInt32 dataSize;
  883.     UInt32 count;
  884.     UInt32 elemItr, itr;
  885.     ISpElementReference elements[MAX_ELEMENTS];
  886.     UInt8 state[MAX_DATASIZE];
  887.     
  888.     printf("how many elements>");
  889.     gets( theString );
  890.     elementCount = atoi( theString );
  891.  
  892.     printf("data size>");
  893.     gets( theString );
  894.     dataSize = atoi( theString );
  895.  
  896.     printf("count>");
  897.     gets( theString );
  898.     count = atoi( theString );
  899.  
  900.     printf("%ld elements, data size = %ld, count = %ld\n", elementCount, dataSize, count);
  901.     
  902.     
  903.     if ((elementCount > MAX_ELEMENTS) || (dataSize > MAX_DATASIZE))
  904.     {
  905.         printf("••• MAX_ELEMENTS = %d MAX_DATASIZE = %d\n",MAX_ELEMENTS,MAX_DATASIZE);
  906.         return;
  907.     }
  908.  
  909.     for(elemItr = 0; elemItr < elementCount; elemItr++)
  910.     {
  911.         err = ISpElement_NewVirtual(dataSize, &(elements[elemItr]), 0);
  912.  
  913.         if (err)
  914.         {
  915.             printf("••• ISpElement_NewVirtual returned error (%d)\n",err);
  916.             
  917.             err = ISpElement_DisposeVirtual(elemItr, elements);
  918.             if (err)
  919.             {
  920.                 printf("••• ISpElement_DisposeVirtual returned error (%d)\n",err);
  921.             }
  922.             
  923.             return;
  924.         }
  925.     }
  926.     
  927.     for(itr = 0; itr < count; itr++)
  928.     {
  929.         AbsoluteTime time = {0, TickCount() };
  930.         
  931.         if ((itr % 100) == 0) { printf("."); }
  932.         if ((itr % 1000) == 0) 
  933.         {
  934.             long normal = FreeMem();
  935.             long system = FreeMemSys();
  936.             Ptr temp = NewPtr(1000);
  937.             DisposePtr(temp);
  938.             printf("%ld / %ld (app = %ld) (sys = %ld) \n",itr,count,normal,system);
  939.             
  940.         }
  941.         
  942.         SIOUXHandleOneEvent(nil);
  943.         
  944.         for(elemItr = 0; elemItr < elementCount; elemItr++)
  945.         {
  946.             err = ISpElement_PushComplexData(elements[elemItr], dataSize, &state, &time);
  947.             
  948.             if (err)
  949.             {
  950.                 printf("••• ISpElement_PushComplexData returned error (%d)\n",err);
  951.                 return;
  952.             }
  953.         }
  954.     }
  955.  
  956.     printf("\ndisposing elements\n");
  957.     err = ISpElement_DisposeVirtual(elementCount, elements);
  958.     if (err)
  959.     {
  960.         printf("••• ISpElement_DisposeVirtual returned error (%d)\n",err);
  961.     }
  962. }
  963. #endif /* DEBUG_DRIVER */
  964.  
  965. static void TestArbitraryAllocate(void)
  966. {
  967.     char theString[32];
  968.     OSErr err = noErr;
  969.     UInt32 elementCount = 0;
  970.     UInt32 dataSize;
  971.     UInt32 elemItr;
  972.     ISpElementReference elements[MAX_ELEMENTS];
  973.     
  974.     printf("how many elements>");
  975.     gets( theString );
  976.     elementCount = atoi( theString );
  977.  
  978.     printf("data size>");
  979.     gets( theString );
  980.     dataSize = atoi( theString );
  981.  
  982.  
  983.     printf("%ld elements, data size = %ld\n", elementCount, dataSize);    
  984.     
  985.     if ((elementCount > MAX_ELEMENTS) || (dataSize > MAX_DATASIZE))
  986.     {
  987.         printf("••• MAX_ELEMENTS = %d MAX_DATASIZE = %d\n",MAX_ELEMENTS,MAX_DATASIZE);
  988.         return;
  989.     }
  990.  
  991.     printf("allocating elements\n");
  992.     for(elemItr = 0; elemItr < elementCount; elemItr++)
  993.     {
  994.         err = ISpElement_NewVirtual(dataSize, &(elements[elemItr]), 0);
  995.  
  996.         if (err)
  997.         {
  998.             printf("••• ISpElement_NewVirtual returned error (%d)\n",err);
  999.             
  1000.             err = ISpElement_DisposeVirtual(elemItr, elements);
  1001.             if (err)
  1002.             {
  1003.                 printf("••• ISpElement_DisposeVirtual returned error (%d)\n",err);
  1004.             }
  1005.             
  1006.             return;
  1007.         }
  1008.     }
  1009.  
  1010.     printf("\ndisposing elements\n");
  1011.     err = ISpElement_DisposeVirtual(elementCount, elements);
  1012.     if (err)
  1013.     {
  1014.         printf("••• ISpElement_DisposeVirtual returned error (%d)\n",err);
  1015.     }
  1016. }
  1017.  
  1018. #define MTemp 0x828
  1019. #define RawMouse 0x82c
  1020. #define CrsrNewCouple 0x8ce
  1021.  
  1022.  
  1023.  
  1024.  
  1025.  
  1026. void TestAcceleration(void);
  1027. void TestAcceleration(void)
  1028. {
  1029.     TestCreateNeeds();
  1030.  
  1031.     ShowCursor();
  1032.     Point where =  * ( Point * ) RawMouse;
  1033.  
  1034.     float deltaX = 0;
  1035.     float deltaY = 0;
  1036.     
  1037.     while(1)
  1038.     {
  1039.         gVirtualElements[0];
  1040.         gVirtualElements[1];
  1041.         
  1042.         UInt32 forward;
  1043.         UInt32 side;
  1044.         OSStatus status;
  1045.         float temp;
  1046.         SInt8 amt;
  1047.         
  1048.         status = ISpElement_GetSimpleState(gVirtualElements[1], &forward);
  1049.  
  1050.         if (status == noErr)
  1051.         {
  1052.             temp = forward;
  1053.             temp -= kISpAxisMiddle;
  1054.             temp /= kISpAxisMiddle;
  1055.             temp *= 25;
  1056.             
  1057.             deltaX += temp;
  1058.             
  1059.             if (deltaX >= 1) 
  1060.             {
  1061.                 amt = deltaX;
  1062.                 where.h += amt;
  1063.                 deltaX -= amt;
  1064.             }
  1065.             else if (deltaX <= -1)
  1066.             {
  1067.                 amt = -deltaX;
  1068.                 where.h -= amt;
  1069.                 deltaX += amt;
  1070.             }
  1071.         }
  1072.         
  1073.         status = ISpElement_GetSimpleState(gVirtualElements[0], &side);
  1074.  
  1075.         if (status == noErr)
  1076.         {
  1077.             temp = side;
  1078.             temp -= kISpAxisMiddle;
  1079.             temp /= kISpAxisMiddle;
  1080.             temp *= -25;
  1081.             
  1082.             deltaY += temp;
  1083.  
  1084.             if (deltaY >= 1) 
  1085.             {
  1086.                 amt = deltaY;
  1087.                 where.v += amt;
  1088.                 deltaY -= amt;
  1089.             }
  1090.             else if (deltaY <= -1)
  1091.             {
  1092.                 amt = -deltaY;
  1093.                 where.v -= amt;
  1094.                 deltaY += amt;
  1095.             }
  1096.  
  1097.         }
  1098.                 
  1099.         HideCursor();
  1100.         
  1101.         * ( Point * ) RawMouse = where ;
  1102.         * ( Point * ) MTemp = where ;
  1103.         * ( short * ) CrsrNewCouple = -1 ;
  1104.         
  1105.         ShowCursor();
  1106.     
  1107.         KeyMap theKeys;
  1108.         GetKeys(theKeys);
  1109.  
  1110.         if ((theKeys[1] & 0x8000))
  1111.         {
  1112.             break;
  1113.         }
  1114.         
  1115.         unsigned long aLong = ::TickCount();
  1116.         
  1117.         while (aLong == TickCount()) { ; }
  1118.     }
  1119. }
  1120.  
  1121. #define FVERIFY(x)    if (x) { FailCode(x); return false; }
  1122. #define FVERIFY2(x, y)    if (x) {    char VERIFY2_DEBUG_STATUS[512]; \
  1123.                                     sprintf(VERIFY2_DEBUG_STATUS,"%s got %d",y,x); \
  1124.                                      FailMsg(VERIFY2_DEBUG_STATUS); return false; }
  1125.  
  1126. // true if ExtractByClass works
  1127. // false otherwise
  1128. // 
  1129. static Boolean VerifyExtractByClass(void)
  1130. {
  1131.     ISpDeviceClass deviceClass;
  1132.     const UInt32 kNumDeviceClass = 8;
  1133.     ISpDeviceClass kDeviceClassArray[kNumDeviceClass] = { kISpDeviceClass_SpeechRecognition,
  1134.                                     kISpDeviceClass_Mouse,
  1135.                                     kISpDeviceClass_Keyboard,
  1136.                                     kISpDeviceClass_Joystick,
  1137.                                     kISpDeviceClass_Wheel,
  1138.                                     kISpDeviceClass_Pedals,
  1139.                                     kISpDeviceClass_Levers,
  1140.                                     'junk'
  1141.                                     };
  1142.     UInt32 deviceClassItr;
  1143.     OSStatus err;
  1144.     UInt32 result1, result2, result3;
  1145.     const UInt32 kFixedBufferSize = 100;
  1146.     ISpDeviceReference fixedBuffer[kFixedBufferSize] = { 0 } ;
  1147.     ISpDeviceReference *exactBuffer;
  1148.     UInt32 verifyItr;
  1149.     char debug_status[255];
  1150.     
  1151.     NextStep("broad band ISpDevices_ExtractByClass verification");
  1152.     
  1153.     for(deviceClassItr = 0; deviceClassItr < kNumDeviceClass; deviceClassItr++)
  1154.     {    
  1155.         deviceClass = kDeviceClassArray[deviceClassItr];
  1156.         
  1157.         sprintf(debug_status, "device class = %c%c%c%c", (deviceClass & 0xff000000) >> 24, (deviceClass & 0xff0000) >> 16, 
  1158.                 (deviceClass & 0xff00) >> 8, (deviceClass & 0xff) >> 0);
  1159.         StatusMsg(debug_status);
  1160.         
  1161.         // result 1
  1162.         err = ISpDevices_ExtractByClass(deviceClass, 0, &result1, NULL);
  1163.         if (err) { FailMsg("ISpDevices_ExtractByClass failed with a null buffer"); return false; }
  1164.         
  1165.         sprintf(debug_status, "found %d devices of that type",result1);
  1166.         StatusMsg(debug_status);
  1167.         
  1168.         // result 2
  1169.         err = ISpDevices_ExtractByClass(deviceClass, kFixedBufferSize, &result2, fixedBuffer);
  1170.         if (err) { FailMsg("ISpDevices_ExtractByClass failed with a fixed buffer"); return false; }
  1171.         for(verifyItr = 0; ((verifyItr < result2) && (verifyItr < kFixedBufferSize)); verifyItr ++)
  1172.         {
  1173.             StatusMsg("verifying on list 2");
  1174.             if (fixedBuffer[verifyItr] == nil) 
  1175.             {
  1176.                 FailMsg("ISpDevices_ExtractByClass failed to verify non nil with a fixed buffer");
  1177.                 return false;
  1178.             }
  1179.         }
  1180.         
  1181.         // result 3
  1182.         exactBuffer = (ISpDeviceReference *) NewPtrClear(sizeof(ISpDeviceReference) * result1);
  1183.         if (exactBuffer == nil) { FailMsg("Problem with test program not the sprocket, test program needs more memory!"); return false; }
  1184.         err = ISpDevices_ExtractByClass(deviceClass, result1, &result3, exactBuffer);
  1185.         if (err) { FailMsg("ISpDevices_ExtractByClass failed with an exact buffer"); return false; }
  1186.         for(verifyItr = 0; verifyItr < result3; verifyItr ++)
  1187.         {
  1188.             StatusMsg("verifying on list 3");
  1189.             if (exactBuffer[verifyItr] == nil) 
  1190.             {
  1191.                 FailMsg("ISpDevices_ExtractByClass failed to verify non nil with an exact buffer");
  1192.                 return false;
  1193.             }
  1194.         }
  1195.         DisposePtr((Ptr) exactBuffer);
  1196.         
  1197.         if ((result1 != result2) || (result1 != result3))
  1198.         {
  1199.             FailMsg("the three results were not all the same!");
  1200.             return false;
  1201.         }
  1202.     }
  1203.     
  1204.     return true;
  1205. }
  1206.  
  1207.  
  1208. static Boolean VerifyActivateDeactive(void)
  1209. {
  1210.     const UInt32 kMaxDevices = 100;
  1211.     ISpDeviceReference devices[kMaxDevices];
  1212.     OSStatus err;
  1213.     UInt32 numDevices;
  1214.     char debug_status[512];
  1215.     NextStep("Running Enable/Disable test");
  1216.     UInt32 deviceItr;
  1217.     UInt32 enableDisableItr;
  1218.     const UInt32 kNumEnableDisableAttempts = 1000;
  1219.     
  1220.     err = ISpDevices_Extract(kMaxDevices, &numDevices, devices);
  1221.     FVERIFY2(err,"ISpDevices_Extract")
  1222.         
  1223.     for(deviceItr = 0; deviceItr < numDevices; deviceItr++)
  1224.     {
  1225.         ISpDeviceReference thisDevice = devices[deviceItr];
  1226.         Boolean wasActive;
  1227.         ISpDeviceDefinition thisDefinition;
  1228.         
  1229.         err = ISpDevice_IsActive(thisDevice, &wasActive);
  1230.         FVERIFY2(err, "ISpDevice_IsActive");
  1231.         
  1232.         if (wasActive)
  1233.         {
  1234.             err = ISpDevices_Deactivate(1, &thisDevice);
  1235.             FVERIFY2(err, "ISpDevices_Deactivate")
  1236.         }
  1237.         
  1238.         err = ISpDevice_GetDefinition(thisDevice, sizeof(ISpDeviceDefinition), &thisDefinition);
  1239.         FVERIFY2(err, "ISpDevice_GetDefinition")
  1240.         
  1241.         sprintf(debug_status,"working on device ");
  1242.         AppendStr63(debug_status, thisDefinition.deviceName);
  1243.         StatusMsg(debug_status);
  1244.         
  1245.         for(enableDisableItr = 0; enableDisableItr < kNumEnableDisableAttempts; enableDisableItr++)
  1246.         {
  1247.         
  1248.             if ((enableDisableItr % (kNumEnableDisableAttempts / 10)) == 0)
  1249.             {
  1250.                 sprintf(debug_status, "%d%\%", enableDisableItr / (kNumEnableDisableAttempts / 100));
  1251.                 StatusMsg(debug_status);
  1252.             }
  1253.             
  1254.             err = ISpDevices_Activate(1, &thisDevice);
  1255.             FVERIFY2(err, "ISpDevices_Activate")
  1256.  
  1257.             err = ISpDevices_Deactivate(1, &thisDevice);
  1258.             FVERIFY2(err, "ISpDevices_Deactivate")            
  1259.         }
  1260.         
  1261.         StatusMsg("100%");
  1262.         
  1263.         if (wasActive)
  1264.         {
  1265.             err = ISpDevices_Activate(1, &thisDevice);
  1266.             FVERIFY2(err, "ISpDevices_Activate")
  1267.         }
  1268.     }    
  1269.  
  1270.     return true;    
  1271. }
  1272.  
  1273. static void WaitForAnyKey(void)
  1274. {
  1275.     KeyMap theKeyMap;
  1276.     
  1277.     GetKeys(theKeyMap);
  1278.     
  1279.     while((theKeyMap[0] == 0) && (theKeyMap[1] == 0) && (theKeyMap[2] == 0) && (theKeyMap[3] == 0))
  1280.     {
  1281.         SIOUXHandleOneEvent(nil);
  1282.         GetKeys(theKeyMap);
  1283.     }
  1284. }
  1285.  
  1286. #define VERIFY(x)    if (x) { FailCode(x); return; }
  1287.  
  1288. static void AutoTest(void)
  1289. {
  1290.     OSStatus err;
  1291.     char debug_status[512];
  1292.     debug_status;
  1293.     Boolean success;
  1294.     long app_memory, sys_memory;
  1295.     long app_memory_final, sys_memory_final;
  1296.     long app_memory_delta, sys_memory_delta;
  1297.  
  1298.     InitStep("press any key to begin AutoTest");
  1299.     WaitForAnyKey();
  1300.     
  1301.     NextStep("verifying that this is the first initialization");
  1302.     {
  1303.         if (gVirtualList != NULL)
  1304.         {
  1305.             FailMsg("this was not the first time a test was run for this launch of the application");
  1306.             return;
  1307.         }
  1308.     }
  1309.     
  1310.     NextStep("starting up");
  1311.     {
  1312.         err = ISpStartup();
  1313.         VERIFY(err);
  1314.     }
  1315.     
  1316.     NextStep("shutting down");
  1317.     {
  1318.         err = ISpShutdown();
  1319.         VERIFY(err);
  1320.     }
  1321.     
  1322.     NextStep("looping on startup / shutdown");
  1323.     {
  1324.         const UInt32 kStartShutAttempts = 20;
  1325.         UInt32 startShutItr;
  1326.                 
  1327.         for(startShutItr = 0; startShutItr < kStartShutAttempts; startShutItr++)
  1328.         {
  1329.             if ((startShutItr % (kStartShutAttempts / 10)) == 0)
  1330.             {
  1331.                 sprintf(debug_status, "%d0%%", startShutItr / (kStartShutAttempts / 10));
  1332.                 StatusMsg(debug_status);
  1333.             }
  1334.  
  1335.             err = ISpStartup();
  1336.             VERIFY(err);
  1337.  
  1338.             err = ISpShutdown();
  1339.             VERIFY(err)
  1340.         }
  1341.     
  1342.         err = ISpStartup();
  1343.         VERIFY(err);
  1344.     }
  1345.     
  1346.     NextStep("single ISpSuspend (low level)");
  1347.     err = ISpSuspend();
  1348.     VERIFY(err);
  1349.  
  1350.     NextStep("single ISpResume (low level)");
  1351.     err = ISpResume();
  1352.     VERIFY(err);
  1353.  
  1354.     NextStep("testing suspend/resume repeatedly (low level)");
  1355.     {
  1356.         UInt32 kSuspendResumeLLCount = 100;
  1357.         UInt32 suspendResumeLLItr;
  1358.                 
  1359.         err = ISpSuspend();
  1360.         VERIFY(err);
  1361.             
  1362.         err = ISpResume();
  1363.         VERIFY(err);
  1364.  
  1365.  
  1366.         for(suspendResumeLLItr = 0; suspendResumeLLItr < kSuspendResumeLLCount;  suspendResumeLLItr++)
  1367.         {
  1368.             if ((suspendResumeLLItr % (kSuspendResumeLLCount / 10)) == 0)
  1369.             {
  1370.                 sprintf(debug_status, "%d%\%", suspendResumeLLItr / (kSuspendResumeLLCount / 100));
  1371.                 StatusMsg(debug_status);
  1372.             }
  1373.             err = ISpSuspend();
  1374.             VERIFY(err);
  1375.             
  1376.             err = ISpResume();
  1377.             VERIFY(err);
  1378.         }        
  1379.     }
  1380.  
  1381.     NextStep("creating virtual elements");
  1382.       {
  1383.           err = ISpElement_NewVirtualFromNeeds(kNeedCount, gNeeds, gVirtualElements, 0);
  1384.     
  1385.         VERIFY(err)
  1386.     }
  1387.     
  1388.     NextStep("creating an element list");
  1389.     {
  1390.         err = ISpElementList_New(
  1391.             kNeedCount,                // count
  1392.             gVirtualElements,        // needs
  1393.             &gVirtualList,            // virtual elements
  1394.             0);                        // flags
  1395.     
  1396.         VERIFY(err)
  1397.     }
  1398.     
  1399.     NextStep("initializing input sprocket once");
  1400.     {
  1401.         err = ISpInit(kNeedCount,    // count
  1402.                 gNeeds,                // needs
  1403.                 gVirtualElements,    // virtual elements
  1404.                 'ISpT',                // app
  1405.                 'foob',                // sub
  1406.                 0,                    // flags
  1407.                 128,                // set list resource id
  1408.                 0);                    // version
  1409.  
  1410.         VERIFY(err)
  1411.     }
  1412.     
  1413.     NextStep("looping on ISpInit / ISpStop");
  1414.     {
  1415.         const UInt32 kStartStopAttempts = 20;
  1416.         UInt32 startStopItr;
  1417.                 
  1418.         err = ISpStop();
  1419.         VERIFY(err)
  1420.  
  1421.         for(startStopItr = 0; startStopItr < kStartStopAttempts; startStopItr++)
  1422.         {
  1423.             if ((startStopItr % (kStartStopAttempts / 10)) == 0)
  1424.             {
  1425.                 sprintf(debug_status, "%d0%%", startStopItr / (kStartStopAttempts / 10));
  1426.                 StatusMsg(debug_status);
  1427.             }
  1428.  
  1429.             app_memory = FreeMem();
  1430.             sys_memory = FreeMemSys();
  1431.  
  1432.             err = ISpInit(kNeedCount, gNeeds, gVirtualElements, 'ISpT', 'foob', 0, 128, 0);
  1433.             VERIFY(err);
  1434.  
  1435.             err = ISpStop();
  1436.             VERIFY(err)
  1437.  
  1438.             app_memory_final = FreeMem();
  1439.             sys_memory_final = FreeMemSys();
  1440.             
  1441.             app_memory_delta = app_memory - app_memory_final;
  1442.             sys_memory_delta = sys_memory - sys_memory_final;
  1443.  
  1444. #if 0
  1445.             if (app_memory_delta > 0)
  1446.             {
  1447.                 sprintf(debug_status, "app memory leak: start free = %d end free = %d leak = %d", 
  1448.                         app_memory, app_memory_final, app_memory_delta);
  1449.                 FAILMSG(debug_status);
  1450.             }
  1451.             
  1452.             if (sys_memory_delta > 0)
  1453.             {
  1454.                 sprintf(debug_status, "sys memory leak: start free = %d end free = %d leak = %d", 
  1455.                         sys_memory, sys_memory_final, sys_memory_delta);
  1456.                 FAILMSG(debug_status);
  1457.             }
  1458. #endif
  1459.         }
  1460.     
  1461.         err = ISpInit(kNeedCount, gNeeds, gVirtualElements, 'ISpT', 'foob', 0, 128, 0);
  1462.         VERIFY(err);
  1463.     }
  1464.     
  1465.     NextStep("counting all the devices (0 size buffer)");
  1466.     {
  1467.         UInt32 deviceCount;
  1468.         
  1469.         err = ISpDevices_Extract(0, &deviceCount, nil);
  1470.         
  1471.         VERIFY(err)
  1472.     }
  1473.     
  1474.     NextStep("counting all the devices (100 size buffer)");
  1475.     {
  1476.         UInt32 deviceCount;
  1477.         ISpDeviceReference buffer[100];
  1478.         
  1479.         err = ISpDevices_Extract(100, &deviceCount, buffer);
  1480.         
  1481.         VERIFY(err)
  1482.     }
  1483.     
  1484.     NextStep("verifying all the devices (100 size buffer) w/ ISpDevice_IsActive");
  1485.     {
  1486.         UInt32 deviceCount;
  1487.         ISpDeviceReference buffer[100];
  1488.         UInt32 itr;
  1489.         Boolean isActive;
  1490.         
  1491.         err = ISpDevices_Extract(100, &deviceCount, buffer);
  1492.         
  1493.         if (!err)
  1494.         {
  1495.             for(itr = 0; itr < deviceCount; itr++)
  1496.             {
  1497.                 err = ISpDevice_IsActive(buffer[itr], &isActive);
  1498.                 if (err) { break; }
  1499.             }
  1500.         }
  1501.                     
  1502.         VERIFY(err)
  1503.     }
  1504.     
  1505.     NextStep("verifying all the devices (100 size buffer) w/ ISpDevice_GetDefinition");
  1506.     {
  1507.         UInt32 deviceCount;
  1508.         ISpDeviceReference buffer[100];
  1509.         UInt32 itr;
  1510.         ISpDeviceDefinition outStruct;
  1511.         
  1512.         err = ISpDevices_Extract(100, &deviceCount, buffer);
  1513.         
  1514.         if (!err)
  1515.         {
  1516.             for(itr = 0; itr < deviceCount; itr++)
  1517.             {
  1518.                 err = ISpDevice_GetDefinition(buffer[itr], sizeof(ISpDeviceDefinition), &outStruct);
  1519.                 if (err) { break; }
  1520.             }
  1521.         }
  1522.                     
  1523.         VERIFY(err)
  1524.     }
  1525.     
  1526.     NextStep("searching for all the devices of class kISpDeviceClass_Mouse");
  1527.     {
  1528.         UInt32 deviceCount;
  1529.         ISpDeviceReference buffer[100];
  1530.         
  1531.         err = ISpDevices_ExtractByClass(kISpDeviceClass_Mouse, 100, &deviceCount, buffer);
  1532.         
  1533.         VERIFY(err)
  1534.     }
  1535.  
  1536.     NextStep("searching for all the devices of class kISpDeviceClass_Keyboard");
  1537.     {
  1538.         UInt32 deviceCount;
  1539.         ISpDeviceReference buffer[100];
  1540.         
  1541.         err = ISpDevices_ExtractByClass(kISpDeviceClass_Keyboard, 100, &deviceCount, buffer);
  1542.         
  1543.         VERIFY(err)
  1544.     }
  1545.  
  1546.     NextStep("searching for all the devices of class kISpDeviceClass_Joystick");
  1547.     {
  1548.         UInt32 deviceCount;
  1549.         ISpDeviceReference buffer[100];
  1550.         
  1551.         err = ISpDevices_ExtractByClass(kISpDeviceClass_Joystick, 100, &deviceCount, buffer);
  1552.         
  1553.         VERIFY(err)
  1554.     }
  1555.     
  1556.     success = VerifyExtractByClass();
  1557.     if (!success) { return; }
  1558.     
  1559.     success = VerifyActivateDeactive();
  1560.     if (!success) { return; }
  1561.     
  1562.     NextStep("bringing up the configuration screen (devices disabled, dialog warning you there are no devices is OK)");
  1563.     {
  1564.         err = ISpConfigure(nil);
  1565.         
  1566.         VERIFY(err)
  1567.     }
  1568.     
  1569.     NextStep("enabling all the devices of class kISpDeviceClass_Mouse");
  1570.     {
  1571.         UInt32 deviceCount;
  1572.         ISpDeviceReference buffer[100];
  1573.  
  1574.         err = ISpDevices_ExtractByClass(kISpDeviceClass_Mouse, 100, &deviceCount, buffer);
  1575.         
  1576.         if (!err)
  1577.         {
  1578.             err = ISpDevices_Activate(deviceCount, buffer);
  1579.         }
  1580.         
  1581.         VERIFY(err)
  1582.     }
  1583.  
  1584.     NextStep("enabling all the devices of class kISpDeviceClass_Keyboard");
  1585.     {
  1586.         UInt32 deviceCount;
  1587.         ISpDeviceReference buffer[100];
  1588.         
  1589.         err = ISpDevices_ExtractByClass(kISpDeviceClass_Keyboard, 100, &deviceCount, buffer);
  1590.         
  1591.         if (!err)
  1592.         {
  1593.             err = ISpDevices_Activate(deviceCount, buffer);
  1594.         }
  1595.  
  1596.         VERIFY(err)
  1597.     }
  1598.     
  1599.     NextStep("bringing up the configuration screen");
  1600.     {
  1601.         err = ISpConfigure(nil);
  1602.         
  1603.         VERIFY(err)
  1604.     }
  1605.     
  1606.     NextStep("bringing up the configuration screen w/ filter proc");
  1607.     {
  1608.         err = ISpConfigure(ISpConfigureFilterProc);
  1609.         
  1610.         VERIFY(err)
  1611.     }
  1612.     
  1613.     NextStep("getting the global list");
  1614.     {
  1615.         ISpElementListReference globalList;
  1616.         
  1617.         err = ISpGetGlobalElementList(&globalList);
  1618.  
  1619.         VERIFY(err)
  1620.     }
  1621.     
  1622.     NextStep("flushing the global list (and pausing)");
  1623.     {
  1624.         ISpElementListReference globalList;
  1625.         unsigned long ticks;
  1626.         const number_of_seconds_to_pause = 3;
  1627.         
  1628.         err = ISpGetGlobalElementList(&globalList);
  1629.  
  1630.         if (!err)
  1631.         {
  1632.             ticks = TickCount();
  1633.             ticks += number_of_seconds_to_pause * 60;
  1634.             
  1635.             while(ticks > TickCount())
  1636.             {
  1637.                 err = ISpElementList_Flush(globalList);
  1638.                 if (err) { break; }
  1639.             }
  1640.         }
  1641.  
  1642.         VERIFY(err)
  1643.     }
  1644.     
  1645.     NextStep("testing getting events, please press a button or move an axis");
  1646.     {
  1647.         ISpElementListReference globalList;
  1648.         ISpElementEvent event;
  1649.         Boolean wasEvent;
  1650.         
  1651.         err = ISpGetGlobalElementList(&globalList);
  1652.         
  1653.         if (!err)
  1654.         {
  1655.             wasEvent = false;
  1656.             
  1657.             while(!wasEvent)
  1658.             {
  1659.                 err = ISpElementList_GetNextEvent(globalList, sizeof(ISpElementEvent), &event, &wasEvent);
  1660.                 
  1661.                 if (err == kISpBufferToSmallErr) { err = noErr; }
  1662.                 
  1663.                 if (err) { break; }
  1664.             }
  1665.         }
  1666.         
  1667.         VERIFY(err)
  1668.     }
  1669.  
  1670.  
  1671.     NextStep("flushing the need list (and pausing)");
  1672.     {
  1673.         unsigned long ticks;
  1674.         const number_of_seconds_to_pause = 3;
  1675.         
  1676.         ticks = TickCount();
  1677.         ticks += number_of_seconds_to_pause * 60;
  1678.         
  1679.         while(ticks > TickCount())
  1680.         {
  1681.             err = ISpElementList_Flush(gVirtualList);
  1682.             if (err) { break; }
  1683.         }
  1684.  
  1685.         VERIFY(err)
  1686.     }
  1687.     
  1688.     NextStep("testing getting need events, please press a configured button or move an axis (escape should always work)");
  1689.     {
  1690.         ISpElementEvent event;
  1691.         Boolean wasEvent;
  1692.         
  1693.         wasEvent = false;
  1694.         
  1695.         while(!wasEvent)
  1696.         {
  1697.             err = ISpElementList_GetNextEvent(gVirtualList, sizeof(ISpElementEvent), &event, &wasEvent);
  1698.             
  1699.             if (err) { break; }
  1700.         }
  1701.         
  1702.         VERIFY(err)
  1703.     }
  1704.     
  1705.     NextStep("single ISpSuspend");
  1706.     err = ISpSuspend();
  1707.     VERIFY(err);
  1708.  
  1709.     NextStep("single ISpResume");
  1710.     err = ISpResume();
  1711.     VERIFY(err);
  1712.  
  1713.     NextStep("testing suspend/resume repeatedly (high level)");
  1714.     {
  1715.         UInt32 kSuspendResumeCount = 100;
  1716.         UInt32 suspendResumeItr;
  1717.                 
  1718.         err = ISpSuspend();
  1719.         VERIFY(err);
  1720.             
  1721.         err = ISpResume();
  1722.         VERIFY(err);
  1723.  
  1724.  
  1725.         for(suspendResumeItr = 0; suspendResumeItr < kSuspendResumeCount;  suspendResumeItr++)
  1726.         {
  1727.             if ((suspendResumeItr % (kSuspendResumeCount / 10)) == 0)
  1728.             {
  1729.                 sprintf(debug_status, "%d%\%", suspendResumeItr / (kSuspendResumeCount / 100));
  1730.                 StatusMsg(debug_status);
  1731.             }
  1732.             err = ISpSuspend();
  1733.             VERIFY(err);
  1734.             
  1735.             err = ISpResume();
  1736.             VERIFY(err);
  1737.         }        
  1738.     }
  1739.  
  1740.     NextStep("disabling all the devices of class kISpDeviceClass_Mouse");
  1741.     {
  1742.         UInt32 deviceCount;
  1743.         ISpDeviceReference buffer[100];
  1744.  
  1745.         err = ISpDevices_ExtractByClass(kISpDeviceClass_Mouse, 100, &deviceCount, buffer);
  1746.         
  1747.         if (!err)
  1748.         {
  1749.             err = ISpDevices_Deactivate(deviceCount, buffer);
  1750.         }
  1751.         
  1752.         VERIFY(err)
  1753.     }
  1754.  
  1755.     NextStep("disabling all the devices of class kISpDeviceClass_Keyboard");
  1756.     {
  1757.         UInt32 deviceCount;
  1758.         ISpDeviceReference buffer[100];
  1759.         
  1760.         err = ISpDevices_ExtractByClass(kISpDeviceClass_Keyboard, 100, &deviceCount, buffer);
  1761.         
  1762.         if (!err)
  1763.         {
  1764.             err = ISpDevices_Deactivate(deviceCount, buffer);
  1765.         }
  1766.  
  1767.         
  1768.         VERIFY(err)
  1769.     }
  1770.     
  1771.     NextStep("stopping input sprocket");
  1772.     {
  1773.         err = ISpStop();
  1774.         
  1775.         VERIFY(err)
  1776.     }
  1777. }
  1778.  
  1779. #if AUTOTEST
  1780. void main(void)
  1781. {
  1782.     // tell SIOUX to shut up
  1783.     SIOUXSettings.autocloseonquit = true;
  1784.     SIOUXSettings.asktosaveonclose = false;
  1785.  
  1786.     AutoTest();
  1787.     
  1788.     UInt32 who_cares;    
  1789.     scanf("%d",&who_cares);
  1790. }
  1791. #endif
  1792.  
  1793. #if DIALOGTEST
  1794. void main(void)
  1795. {
  1796.     OSErr err;
  1797.     
  1798.     NextStep("verifying that this is the first initialization");
  1799.     {
  1800.         if (gVirtualList != NULL)
  1801.         {
  1802.             FailMsg("this was not the first time a test was run for this launch of the application");
  1803.             return;
  1804.         }
  1805.     }
  1806.     
  1807.     NextStep("creating virtual elements");
  1808.       {
  1809.           err = ISpElement_NewVirtualFromNeeds(kNeedCount, gNeeds, gVirtualElements, 0);
  1810.     
  1811.         VERIFY(err)
  1812.     }
  1813.     
  1814.     NextStep("creating an element list");
  1815.     {
  1816.         err = ISpElementList_New(
  1817.             kNeedCount,                // count
  1818.             gVirtualElements,        // needs
  1819.             &gVirtualList,            // virtual elements
  1820.             0);                        // flags
  1821.     
  1822.         VERIFY(err)
  1823.     }
  1824.     
  1825.     NextStep("initializing input sprocket once");
  1826.     {
  1827.         err = ISpInit(kNeedCount,    // count
  1828.                 gNeeds,                // needs
  1829.                 gVirtualElements,    // virtual elements
  1830.                 'ISpT',                // app
  1831.                 'foob',                // sub
  1832.                 0,                    // flags
  1833.                 128,                // set list resource id
  1834.                 0);                    // version
  1835.  
  1836.         VERIFY(err)
  1837.     }
  1838.     
  1839.     NextStep("enabling all the devices of class kISpDeviceClass_Mouse");
  1840.     {
  1841.         UInt32 deviceCount;
  1842.         ISpDeviceReference buffer[100];
  1843.  
  1844.         err = ISpDevices_ExtractByClass(kISpDeviceClass_Mouse, 100, &deviceCount, buffer);
  1845.         
  1846.         if (!err)
  1847.         {
  1848.             err = ISpDevices_Activate(deviceCount, buffer);
  1849.         }
  1850.         
  1851.         VERIFY(err)
  1852.     }
  1853.  
  1854.     NextStep("enabling all the devices of class kISpDeviceClass_Keyboard");
  1855.     {
  1856.         UInt32 deviceCount;
  1857.         ISpDeviceReference buffer[100];
  1858.         
  1859.         err = ISpDevices_ExtractByClass(kISpDeviceClass_Keyboard, 100, &deviceCount, buffer);
  1860.         
  1861.         if (!err)
  1862.         {
  1863.             err = ISpDevices_Activate(deviceCount, buffer);
  1864.         }
  1865.  
  1866.         VERIFY(err)
  1867.     }
  1868.     
  1869.     NextStep("enabling all the devices of class kISpDeviceClass_SpeechRecognition");
  1870.     {
  1871.         UInt32 deviceCount;
  1872.         ISpDeviceReference buffer[100];
  1873.         
  1874.         err = ISpDevices_ExtractByClass(kISpDeviceClass_SpeechRecognition, 100, &deviceCount, buffer);
  1875.         
  1876.         if (!err)
  1877.         {
  1878.             err = ISpDevices_Activate(deviceCount, buffer);
  1879.         }
  1880.  
  1881.         VERIFY(err)
  1882.     }
  1883.  
  1884.     NextStep("bringing up the configuration screen");
  1885.     {
  1886.         err = ISpConfigure(nil);
  1887.         
  1888.         VERIFY(err)
  1889.     }
  1890.     
  1891.     NextStep("disabling all the devices of class kISpDeviceClass_Mouse");
  1892.     {
  1893.         UInt32 deviceCount;
  1894.         ISpDeviceReference buffer[100];
  1895.  
  1896.         err = ISpDevices_ExtractByClass(kISpDeviceClass_Mouse, 100, &deviceCount, buffer);
  1897.         
  1898.         if (!err)
  1899.         {
  1900.             err = ISpDevices_Deactivate(deviceCount, buffer);
  1901.         }
  1902.         
  1903.         VERIFY(err)
  1904.     }
  1905.  
  1906.     NextStep("disabling all the devices of class kISpDeviceClass_Keyboard");
  1907.     {
  1908.         UInt32 deviceCount;
  1909.         ISpDeviceReference buffer[100];
  1910.         
  1911.         err = ISpDevices_ExtractByClass(kISpDeviceClass_Keyboard, 100, &deviceCount, buffer);
  1912.         
  1913.         if (!err)
  1914.         {
  1915.             err = ISpDevices_Deactivate(deviceCount, buffer);
  1916.         }
  1917.  
  1918.         
  1919.         VERIFY(err)
  1920.     }
  1921.     NextStep("stopping input sprocket");
  1922.     {
  1923.         err = ISpStop();
  1924.         
  1925.         VERIFY(err)
  1926.     }
  1927. }
  1928. #endif
  1929. #if MENUTEST
  1930. void main(void)
  1931. {
  1932.     // tell SIOUX to shut up
  1933.     SIOUXSettings.autocloseonquit = true;
  1934.     SIOUXSettings.asktosaveonclose = false;
  1935.     
  1936.     printf("starting up...\n");
  1937.     
  1938.     Boolean theDoneFlag = false;
  1939.     
  1940.     EnableDeviceClass(kISpDeviceClass_SpeechRecognition, true);
  1941.     
  1942.     while( !theDoneFlag )
  1943.     {
  1944.         UInt32 theChoice;
  1945.         
  1946.         printf("\n\n\n############################################################\n");
  1947.         if (gIsSuspended)
  1948.         {
  1949.             printf("• SUSPENDED •\n");
  1950.         }
  1951.         
  1952.         if (gVirtualList != NULL)
  1953.         {
  1954.             printf("• NEEDS ARE ACTIVE •\n");
  1955.         }
  1956.         
  1957.         printf("Please select from the following tests:\n");        
  1958.         printf("\t 1. Exit\n");
  1959.         printf("\t 2. Run all tests automatically\n");
  1960. //        printf("\t 3. Dump information needed for InputSprocket bug reports\n");
  1961.         printf("\t 4. List all devices\n");
  1962.         printf("\t 5. List all elements\n");
  1963.         printf("\t 6. List all buttons\n");
  1964.         printf("\t 7. List all directional pads\n");
  1965.         printf("\t 8. List all axes\n");
  1966.         printf("\t 9. List all X-axes\n");
  1967.         printf("\t10. Get events (stop when Control key pressed)\n");
  1968.         printf("\t11. Suspend\n");
  1969.         printf("\t12. Resume\n");
  1970.         printf("\t13. Create needs\n");
  1971.         printf("\t14. Destroy needs\n");
  1972.         printf("\t15. Get needs events\n");
  1973.         printf("\t16. Configuration dialog\n");
  1974.         printf("\t17. Enable mouse and keyboard\n");
  1975.         printf("\t18. Disable mouse and keyboard\n");
  1976.         printf("\t19. Reinitialize ADB bus\n");
  1977.         printf("\t30. Allocate/Deallocate test\n");
  1978.         #if DEBUG_DRIVER
  1979.             printf("\t31. TestAllocatePushData test\n");
  1980.         #endif
  1981.         printf("\t32. TestArbitraryAllocate test\n");
  1982.         printf("\t33. TestAccelration\n");
  1983.         printf("\t34. Enable speech\n");
  1984.         printf("\t35. Disable speech\n");
  1985.         
  1986.         printf("\n\tSelection: ");
  1987.         
  1988.         Boolean keyboardEnabled = gKeyboardEnabled;
  1989.         if (keyboardEnabled)
  1990.         {
  1991.             EnableDeviceClass(kISpDeviceClass_Keyboard, false);
  1992.         }
  1993.         
  1994.         char theString[32];
  1995.         gets( theString );
  1996.         theChoice = atoi( theString );
  1997.         
  1998.         if (keyboardEnabled)
  1999.         {
  2000.             EnableDeviceClass(kISpDeviceClass_Keyboard, true);
  2001.         }
  2002.         
  2003.         printf("\n\n");
  2004.         
  2005.         switch( theChoice )
  2006.         {
  2007.             case 1:
  2008.                 theDoneFlag = true;
  2009.                 break;
  2010.                 
  2011.             case 2:
  2012.                 AutoTest();
  2013.                 break;
  2014.                 
  2015.             case 4:
  2016.                 TestListOfDevices();
  2017.                 break;            
  2018.             
  2019.             case 5:
  2020.                 TestListOfElements();
  2021.                 break;            
  2022.             
  2023.             case 6:
  2024.                 TestListOfButtons();
  2025.                 break;            
  2026.             
  2027.             case 7:
  2028.                 TestListOfDPads();
  2029.                 break;            
  2030.             
  2031.             case 8:
  2032.                 TestListOfAxes();
  2033.                 break;            
  2034.             
  2035.             case 9:
  2036.                 TestListOfXAxes();
  2037.                 break;            
  2038.             
  2039.             case 10:
  2040.                 GetAndPrintEvents(NULL);
  2041.                 break;            
  2042.             
  2043.             case 11:
  2044.                 TestSuspend();
  2045.                 break;            
  2046.             
  2047.             case 12:
  2048.                 TestResume();
  2049.                 break;            
  2050.             
  2051.             case 13:
  2052.                 TestCreateNeeds();
  2053.                 break;            
  2054.             
  2055.             case 14:
  2056.                 TestDestroyNeeds();
  2057.                 break;            
  2058.             
  2059.             case 15:
  2060.                 TestNeedsEvents();
  2061.                 break;            
  2062.             
  2063.             case 16:
  2064.                 TestConfiguration();
  2065.                 break;            
  2066.             
  2067.             case 17:
  2068.                 EnableDeviceClass(kISpDeviceClass_Mouse, true);
  2069.                 EnableDeviceClass(kISpDeviceClass_Keyboard, true);
  2070.                 break;            
  2071.             
  2072.             case 18:
  2073.                 EnableDeviceClass(kISpDeviceClass_Mouse, false);
  2074.                 EnableDeviceClass(kISpDeviceClass_Keyboard, false);
  2075.                 break;
  2076.             
  2077.             case 19:
  2078.                 ADBReInit();
  2079.                 break;
  2080.                 
  2081.             case 30:
  2082.                 TestAllocateDeallocate();
  2083.                 break;
  2084.         
  2085.         #if DEBUG_DRIVER
  2086.             case 31:
  2087.                 TestAllocatePushData();
  2088.                 break;
  2089.         #endif
  2090.             
  2091.             case 32:
  2092.                 TestArbitraryAllocate();
  2093.                 break;
  2094.             
  2095.             case 33:
  2096.                 TestAcceleration();
  2097.                 break;
  2098.                 
  2099.             case 34:
  2100.                 EnableDeviceClass(kISpDeviceClass_SpeechRecognition, true);
  2101.                 break;            
  2102.             
  2103.             case 35:
  2104.                 EnableDeviceClass(kISpDeviceClass_SpeechRecognition, false);
  2105.                 break;
  2106.             
  2107.             default:
  2108.                 printf("\nhuh?\n");
  2109.                 break;            
  2110.         }
  2111.         
  2112.         printf("\nThank you, please drive through.\n");
  2113.     }
  2114. }
  2115. #endif
  2116.